package com.dinadi.yidi.ui.fragment;

import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;

import com.alibaba.fastjson.JSON;
import com.dinadi.yidi.R;
import com.dinadi.yidi.base.BaseFragment;
import com.dinadi.yidi.bean.SettledBean;
import com.dinadi.yidi.http.HttpListener;
import com.dinadi.yidi.http.interfaces.CommonInterface;
import com.dinadi.yidi.ui.view.CommonDialogUtils;
import com.dinadi.yidi.utils.JsonUtil;
import com.dinadi.yidi.utils.SharePreferencesHelper;
import com.dinadi.yidi.utils.StringUtil;

/**
 * author: JiaYu.He
 * Date: 2017/3/21  入驻
 * Time: 16:35
 */

public class SettledFragment extends BaseFragment {
    /**/
    private View view;
    private ImageView btn_send;
    private EditText edit_name, edit_brand_name, edit_phone;
    private String name, brandName, phone, token;

    @Override
    protected void noWifiRefresh() {

    }

    @Override
    protected View getLayout() {
        view = LayoutInflater.from(getContext()).inflate(R.layout.fragment_settled, null);
        return view;

    }

    @Override
    protected void initView() {
        changePager(2);
        btn_send = (ImageView) view.findViewById(R.id.btn_send);
        edit_name = (EditText) view.findViewById(R.id.edit_name);
        edit_brand_name = (EditText) view.findViewById(R.id.edit_brand_name);
        edit_phone = (EditText) view.findViewById(R.id.edit_phone);


    }

    @Override
    protected void initListener() {
        registerClickListener(btn_send);
    }

    @Override
    protected void clickEvent(View v) {
        switch (v.getId()) {
            case R.id.btn_send:
                /*确认提交*/
                name = edit_name.getText().toString();
                brandName = edit_brand_name.getText().toString();
                phone = edit_phone.getText().toString();
                if (TextUtils.isEmpty(name)) {
                    toast(R.string.toast_name);
                    return;

                } else if (TextUtils.isEmpty(brandName)) {
                    toast(R.string.toast_brand_name);
                    return;

                }
                if (TextUtils.isEmpty(phone)) {
                    toast(R.string.toast_phone);
                    return;
                } else if (!StringUtil.isPhoneNumber(phone)) {
                    toast(R.string.edit_need_phone_no);
                    return;
                }
                gotoSend();

                break;
            default:
                break;
        }

    }

    /*确认提交*/
    private void gotoSend() {
        SettledBean bean = new SettledBean();
        bean.setBrandName(brandName);
        bean.setPhone(phone);
        bean.setPartnerName(name);
        bean.setType("2");
        bean.setToken(token);
        showLoadingDialog(getResources().getString(R.string.dialog_text_send));
        CommonInterface settledInterface = new CommonInterface(getActivity(), httpListener);
        settledInterface.getInterface("11007", JSON.toJSONString(bean));
    }

    @Override
    public void onResume() {
        super.onResume();
        token = SharePreferencesHelper.getInstance(getActivity()).getString("token");
    }

    @Override
    protected void initData() {

    }

    HttpListener httpListener = new HttpListener() {
        @Override
        public void OnSuccess(String response) {
            super.OnSuccess(response);
            String errCode = JsonUtil.getFieldValue(response.toString(), "errCode");
            String errMsg = JsonUtil.getFieldValue(response.toString(), "errMsg");
            if (errCode.equals("0000")) {
                closeLoadingDialog();
                toast("提交成功");
                edit_brand_name.setText("");
                edit_name.setText("");
                edit_phone.setText("");
            }

        }

        @Override
        public void OnFail(String errorResponse) {
            super.OnFail(errorResponse);
            toast("提交失败");
        }
    };

}
